package com.example.hms.service.servicelmpl;

import com.example.hms.dao.BlackListDao;
import com.example.hms.entity.BlackList;
import com.example.hms.service.BlackListService;
import com.example.hms.service.SystemLogService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class BlackListServiceImpl implements BlackListService {
    @Resource
    BlackListDao blackListDao;
    @Resource
    SystemLogService systemLogService;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Result selectAllBlackList(String blackListId,String customerName,  String state) {
        List<BlackList> blackLists = blackListDao.selectAllBlackList(blackListId,customerName, state);
        return ResultSubmit.success(blackLists);
    }

    @Override
    public Result selectBlackList(String customerId, String cancelTime) {
        Map<String, Object> result = blackListDao.selectBlackList(customerId, cancelTime);
        return ResultSubmit.success(result);
    }

    @Override
    public Result insert(BlackList blackList) {
        // 获取债务信息
        String customerId = String.valueOf(blackList.getCustomerId());
        String cancelTime = format.format(new Date());

        Map<String, Object> debtInfo = blackListDao.selectBlackList(customerId, cancelTime);

        BigDecimal debtMoney = (BigDecimal) debtInfo.get("debtMoney");
        Long debtDay = (Long) debtInfo.get("debtDay");


        // 设置债务信息
        blackList.setDebtMoney(BigDecimal.valueOf(debtMoney != null ? debtMoney.doubleValue() : 0));
        blackList.setDebtDay(String.valueOf(debtDay != null ? debtDay : 0));

        if (debtMoney == null) {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("该用户还未退房，不能加入黑名单");
        }

        // 判断是否满足信誉加分条件
        if (debtMoney != null && debtMoney.compareTo(BigDecimal.ZERO) > 0) {
            int addPoints = debtMoney.divideToIntegralValue(new BigDecimal("500")).intValueExact();
            int currentReputation = Integer.parseInt(blackList.getReputation() != null ? blackList.getReputation() : "0");
            int newReputation = currentReputation + addPoints;
            blackList.setReputation(String.valueOf(newReputation));
        }

        blackList.setDataUpdateTime(format.format(new Date()));
        blackList.setBlackListId(Math.abs((int)System.currentTimeMillis()));
        blackList.setDeleteIdentity("未删除");
        int insert = blackListDao.insert(blackList);
        if (insert!=0){
            return ResultSubmit.success("添加成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("添加失败");
        }
    }
    //    修改严重程度与信誉积分（欠款金额与欠款天数与订单表相关联）

    @Override
    public Result update(BlackList blackList) {
        int update1 = blackListDao.update(blackList);
        blackList.setDataUpdateTime(format.format(new Date()));
        blackList.setBlackListId(Math.abs((int)System.currentTimeMillis()));
        blackList.setDeleteIdentity("未删除");
        int insert1 = blackListDao.insert(blackList);
        if (update1==1 || insert1==1){
            return ResultSubmit.success("修改成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }
//    拉出黑名单

    @Override
    public Result delete(BlackList blackList) {
        int delete1 = blackListDao.update(blackList);
        if (delete1!=0){
            return ResultSubmit.success("执行成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("执行失败");
        }
    }
}

